package com.occupation_health.config;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.occupation_health.controller.HelloController;
import com.occupation_health.controller.IndexController;
import com.occupation_health.controller.MonitorController;
import com.occupation_health.controller.UserController;
import com.occupation_health.model.Authority;
import com.occupation_health.model.Role;
import com.occupation_health.model.UserApi;

public class Config extends JFinalConfig {

	Routes routes;// 供Shiro插件使用

	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

	public void configConstant(Constants me) {

		me.setEncoding("utf-8");
		// RequiresGuest，RequiresAuthentication，RequiresUser验证异常，返回HTTP401状态码
		me.setErrorView(401, "/public/view/user/index.html");
		// RequiresRoles，RequiresPermissions授权异常,返回HTTP403状态码
		me.setErrorView(403, "/public/view/user/index.html");
		me.setDevMode(true);
	}

	public void configRoute(Routes me) {
		this.routes = me;
		me.add("/hello", HelloController.class);
		me.add("/user", UserController.class);
		me.add("/index", IndexController.class);
		me.add("/monitor", MonitorController.class);
	}

	public void configEngine(Engine me) {
		me.setDevMode(true);
		me.addSharedFunction("/public/view/template/_layout.html");
		me.setBaseTemplatePath(PathKit.getWebRootPath()+"/public/view/");
	}

	public void configPlugin(Plugins me) {
		me.add(new EhCachePlugin("src/ehcache.xml")); 
		// 数据库插件
		loadPropertyFile("config.properties");
		DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"),
				getProperty("user"), getProperty("password"));
		me.add(dp);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		me.add(arp);
		arp.addMapping("oh_user", UserApi.class);
		arp.addMapping("oh_user", "u_id", UserApi.class);
		arp.addMapping("oh_role", Role.class);
		arp.addMapping("oh_auth", "a_id", Authority.class);
		// shiro 插件
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
		shiroPlugin.setLoginUrl("/hello");//登陆url:未成功则跳转
		shiroPlugin.setSuccessUrl("/hello");//登陆成功url:验证成功自动跳转 
		shiroPlugin.setUnauthorizedUrl("/hello");//授权url：未授权成功自动跳转
		me.add(shiroPlugin);
	}

	public void configInterceptor(Interceptors me) {
		me.add(new ShiroInterceptor());
	}

	public void configHandler(Handlers me) {
	}
}
